package com.edfu.core.contentmgt.service.impl;
import com.edfu.core.contentmgt.jcr.service.api.IContentMgtService;
import com.edfu.core.contentmgt.jcr.service.impl.ContentMgtServiceImpl;
import com.edfu.core.contentmgt.jcr.service.impl.ContentServiceAccessType;
import com.edfu.core.contentmgt.jcr.vo.Content;
import com.edfu.core.contentmgt.jcr.vo.Version;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class ContentManagementTestUtil {

	private IContentMgtService service;

	public ContentManagementTestUtil(String username,
			ContentServiceAccessType accessType) {
		service = new ContentMgtServiceImpl(accessType, username);
	}

	public Content addDirectory(String directoryPath) {
		Content content = service.addDirectory(directoryPath);
//		if (content != null)
//			System.out.println("Directory Name "
//					+ content.getFullyQualifiedContentName()
//					+ " Directory UUID " + content.getContentUUID());
//		else
//			System.out.println("Null Content");
		return content;
	}

	public Content addFile(String fullyQualifiedFileName, File inputFile,
			Map<String, String> properties) throws FileNotFoundException {
		Content fileStored = null;
		InputStream is = new FileInputStream(inputFile);
		fileStored = service.addFile(fullyQualifiedFileName, properties, is);
		return fileStored;
	}

	public Content addFile(String fullyQualifiedFileName,
			InputStream fileStream, Map<String, String> properties) {
		Content fileStored = service.addFile(fullyQualifiedFileName,
				properties, fileStream);
		return fileStored;
	}

	public Content addFile(String fullyQualifiedFileName, InputStream fileStream) {
		Content fileStored = service
				.addFile(fullyQualifiedFileName, fileStream);
//		if (fileStored != null)
//			System.out.println("File Name "
//					+ fileStored.getFullyQualifiedContentName() + " File UUID "
//					+ fileStored.getContentUUID());
		return fileStored;
	}

	public Content addFile(String fullyQualifiedFileName, File inputFile) {
		Content fileStored = service.addFile(fullyQualifiedFileName, inputFile);
//		if (fileStored != null)
//			System.out.println("File Name "
//					+ fileStored.getFullyQualifiedContentName() + " File UUID "
//					+ fileStored.getContentUUID());
		return fileStored;
	}

	public Version addFileVersion(String fullyQualifiedFileName, File inputFile)
			throws FileNotFoundException {
		InputStream is = new FileInputStream(inputFile);
		Version version = service.addVersion(fullyQualifiedFileName, is);
		return version;
	}

	public Version addFileVersion(String fullyQualifiedFileName,
			Map<String, String> properties, File inputFile)
			throws FileNotFoundException {
		InputStream is = new FileInputStream(inputFile);
		Version version = service.addVersion(fullyQualifiedFileName, null,
				properties, is);
		return version;
	}
	

	public Content getFileDetailsFromPath(String fullyQualifiedFileName) {
		return service.getFileDetailsFromPath(fullyQualifiedFileName);
	}
	
	public Version getFileDetailsFromPath(String fullyQualifiedFileName, String versionNumber) {
		return service.getFileDetailsFromPath(fullyQualifiedFileName, versionNumber);
	}
	
	public Version getFileDetailsFromUUID(String baseFileUUID, String versionNumber) {
		return service.getFileDetailsFromUUID(baseFileUUID, versionNumber) ;
	}

	public void deleteFile(String fullyQualifiedFileName) {
		service.deleteFile(fullyQualifiedFileName);

	}

	public void deleteDirectory(String fullyQualifiedDirectoryName) {
		service.deleteDirectory(fullyQualifiedDirectoryName);
	}

	public InputStream getFileStreamFromPath(String fullyQualifiedFileName) {
		return service.getFileAsStreamFromPath(fullyQualifiedFileName);
	}

	public InputStream getFileStreamFromPath(String fullyQualifiedFileName,
			String versionNumber) {
		return service.getFileAsStreamFromPath(fullyQualifiedFileName,
				versionNumber);
	}

	public InputStream getFileStreamFromUUID(String uuid) {
		return service.getFileAsStreamFromUUID(uuid);
	}

	public List<Content> getChildren(String parentAbsolutePath) {
		return service.getChildren(parentAbsolutePath);
	}

	public void deleteVersion(String fullyQualifiedFileName,
			String versionNumber) {
		service.deleteFile(fullyQualifiedFileName, versionNumber);
	}

	public Version addNewPropertiesUsingBaseFilePath(String fullyQualifiedFileName,
			Map<String, String> properties) {
		return service.addFilePropertiesUsingPath(fullyQualifiedFileName,
				properties);
	}
	
	public Version addNewPropertiesUsingBaseFileUUID(String baseFileUUID,
			Map<String, String> properties) {
		return service.addFilePropertiesUsingUUID(baseFileUUID,
				properties);
	}

	public List<Version> getAllVersions(String filePath) {
		return service.getAllVersions(filePath);
	}

	// TODO Cleanup
	// public void searchFile() throws RepositoryException {
	// Session session = JCRFactoryUtil.createSession("default",
	// ContentServiceAccessType.WEBDAV) ;
	// Workspace wsp = session.getWorkspace();
	// QueryManager queryManager = wsp.getQueryManager();
	// Query query =
	// queryManager.createQuery("select * from [nt:unstructured] where employee = 'shekhar' or city='bng'",
	// Query.JCR_SQL2);
	// System.out.println(query.getStatement());
	// query.setOffset( 0 ); // or any other offset
	// query.setLimit( 10 );
	// QueryResult returnValue = query.execute();
	// NodeIterator itr = returnValue.getNodes() ;
	// while ( itr.hasNext()) {
	// Node node = (Node)itr.next();
	// System.out.println(node.getName());
	// }
	// session.logout();
	// }

}
